package gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import model.Drug;
import model.Patient;

import service.Service;

public class PrescriptionMainFrame extends JFrame {
	private JLabel patientLabel;
	private JLabel drugLabel;
	private JLabel prescriptionLabel;
	private JLabel errorLabel;
	private JButton createButton;
	private JButton showButton;
	private JScrollPane scrollPanePersons;
	private JList listPersons;
	private JScrollPane scrollPaneDrug;
	private JList listDrug;
	private JRadioButton pnRadio;
	private JRadioButton irregularRadio;
	private JRadioButton regularRadio;
	private ButtonGroup group;

	private Controller controller = new Controller();

	public PrescriptionMainFrame() {
		Service.getInstance().startup();

		// the frame's own attributes...
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		this.setTitle("Drug description");
		this.setLocation(50, 50);
		this.setSize(500, 360);

		// controls...
		pnRadio = new JRadioButton("PN");
		irregularRadio = new JRadioButton("Daily irregular");
		regularRadio = new JRadioButton("Daily regular");
		group = new ButtonGroup();
		group.add(pnRadio);
		group.add(irregularRadio);
		group.add(regularRadio);

		pnRadio.setSize(100, 15);
		pnRadio.setLocation(350, 80);
		irregularRadio.setSize(100, 15);
		irregularRadio.setLocation(350, 120);
		regularRadio.setSize(100, 15);
		regularRadio.setLocation(350, 160);
		this.add(pnRadio);
		this.add(irregularRadio);
		this.add(regularRadio);
		prescriptionLabel = new JLabel("Choose prescription");
		prescriptionLabel.setSize(160, 22);
		prescriptionLabel.setLocation(350, 15);
		this.add(prescriptionLabel);

		patientLabel = new JLabel("Choose patient");
		patientLabel.setSize(160, 22);
		patientLabel.setLocation(20, 15);
		this.add(patientLabel);
		listPersons = new JList();
		listPersons.setListData(Service.getInstance().getPatients().toArray());
		this.add(listPersons);
		listPersons.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollPanePersons = new JScrollPane(listPersons);
		scrollPanePersons.setLocation(20, 50);
		scrollPanePersons.setSize(160, 190);
		this.add(scrollPanePersons);

		drugLabel = new JLabel("Choose drug");
		drugLabel.setSize(160, 22);
		drugLabel.setLocation(200, 15);
		this.add(drugLabel);
		listDrug = new JList();
		listDrug.setListData(Service.getInstance().getDrugs().toArray());
		this.add(listDrug);
		listDrug.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollPaneDrug = new JScrollPane(listDrug);
		scrollPaneDrug.setLocation(200, 50);
		scrollPaneDrug.setSize(120, 190);
		this.add(scrollPaneDrug);

		createButton = new JButton("Create prescription");
		createButton.setSize(140, 25);
		createButton.setLocation(350, 250);
		this.add(createButton);

		showButton = new JButton("Show prescriptions");
		showButton.setSize(140, 25);
		showButton.setLocation(200, 250);
		this.add(showButton);

		errorLabel = new JLabel("");
		errorLabel.setSize(250, 22);
		errorLabel.setLocation(20, 250);
		this.add(errorLabel);
		errorLabel.setForeground(Color.RED);

		createButton.addActionListener(controller);
		showButton.addActionListener(controller);
	}

	private class Controller implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			errorLabel.setText("");

			if (e.getSource() == createButton) {
				if (listPersons.isSelectionEmpty()) {
					errorLabel.setText("A patient must be chosen");
				} else if (listDrug.isSelectionEmpty()) {
					errorLabel.setText("A drug must be chosen");
				} else if (!pnRadio.isSelected() && !regularRadio.isSelected()
						&& !irregularRadio.isSelected()) {
					errorLabel.setText("A prescription type must be chosen");
				} else if (pnRadio.isSelected() || irregularRadio.isSelected() || regularRadio.isSelected()) {
					PrescriptionType type;
					if (pnRadio.isSelected())
						type = PrescriptionType.PN;
					else if (irregularRadio.isSelected())
						type = PrescriptionType.IRREGULAR;
					else
						type = PrescriptionType.REGULAR;
					Patient p = (Patient) listPersons.getSelectedValue();
					Drug l = (Drug) listDrug.getSelectedValue();
					CreatePrescriptionDialog pnDialog = new CreatePrescriptionDialog(p, l, type);
					pnDialog.setVisible(true);
					if (pnDialog.modalResult) {
					}
				}

			} else if (e.getSource() == showButton) {
				if (listPersons.isSelectionEmpty()) {
					errorLabel.setText("A patient must be chosen");
				} else {
					ShowPrescriptionDialog ordDialog = new ShowPrescriptionDialog(
							(Patient) listPersons.getSelectedValue());
					ordDialog.setVisible(true);
				}
			}
		}
	}
}
